package com.savard.util;

import gnu.io.CommPortIdentifier;
import gnu.io.PortInUseException;
import gnu.io.RXTXPort;
import gnu.io.SerialPort;

import java.util.Collection;
import java.util.Enumeration;
import java.util.Vector;

/**
 * User: csavard
 * Date: 13-03-16
 */
public class SerialPortUtil {
    public static Collection<SerialPort> getSerialPorts() throws PortInUseException {
        Collection<SerialPort> ports = null;
        Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier.getPortIdentifiers();
        while (portEnum.hasMoreElements()) {
            CommPortIdentifier portIdentifier = portEnum.nextElement();
            if (portIdentifier.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                if (ports == null) {
                    ports = new Vector<SerialPort>();
                }

                ports.add(new RXTXPort(portIdentifier.getName()));
            }
        }

        return ports;
    }

}
